{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-06-23T06:59:52.853951Z",
     "start_time": "2025-06-23T06:59:51.671323Z"
    }
   },
   "source": [
    "import torch\n",
    "\n",
    "x = torch.tensor(3.0)\n",
    "y = torch.tensor(2.0)\n",
    "\n",
    "x + y, x * y, x / y, x ** y"
   ],
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(tensor(5.), tensor(6.), tensor(1.5000), tensor(9.))"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 1
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-23T06:59:52.993606Z",
     "start_time": "2025-06-23T06:59:52.978615Z"
    }
   },
   "cell_type": "code",
   "source": [
    "x = torch.arange(4)\n",
    "x"
   ],
   "id": "dd263e95fd79dd00",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([0, 1, 2, 3])"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 2
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-23T06:59:53.039957Z",
     "start_time": "2025-06-23T06:59:53.025893Z"
    }
   },
   "cell_type": "code",
   "source": "x[3]",
   "id": "b473b3af08d3e665",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor(3)"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 3
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-23T06:59:53.071974Z",
     "start_time": "2025-06-23T06:59:53.057706Z"
    }
   },
   "cell_type": "code",
   "source": "x.shape",
   "id": "80f4a0f1f504821a",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.Size([4])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 4
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-23T06:59:53.102947Z",
     "start_time": "2025-06-23T06:59:53.089090Z"
    }
   },
   "cell_type": "code",
   "source": "A = torch.arange(20).reshape(5, 4)",
   "id": "4c7215eeab59161f",
   "outputs": [],
   "execution_count": 5
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-23T06:59:53.132942Z",
     "start_time": "2025-06-23T06:59:53.119076Z"
    }
   },
   "cell_type": "code",
   "source": "A",
   "id": "f1b5f656c4450e9a",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[ 0,  1,  2,  3],\n",
       "        [ 4,  5,  6,  7],\n",
       "        [ 8,  9, 10, 11],\n",
       "        [12, 13, 14, 15],\n",
       "        [16, 17, 18, 19]])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 6
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-23T06:59:53.362863Z",
     "start_time": "2025-06-23T06:59:53.349062Z"
    }
   },
   "cell_type": "code",
   "source": "A.T",
   "id": "872080cf69273355",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[ 0,  4,  8, 12, 16],\n",
       "        [ 1,  5,  9, 13, 17],\n",
       "        [ 2,  6, 10, 14, 18],\n",
       "        [ 3,  7, 11, 15, 19]])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 7
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-23T06:59:53.469872Z",
     "start_time": "2025-06-23T06:59:53.456251Z"
    }
   },
   "cell_type": "code",
   "source": [
    "B = torch.tensor([[1, 2, 3], [2, 0, 4], [3, 4, 5]])\n",
    "B #对称矩阵"
   ],
   "id": "cca0643b54bfb63",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[1, 2, 3],\n",
       "        [2, 0, 4],\n",
       "        [3, 4, 5]])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 8
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-23T06:59:53.625149Z",
     "start_time": "2025-06-23T06:59:53.610863Z"
    }
   },
   "cell_type": "code",
   "source": "B == B.T",
   "id": "1b183479e16d06fe",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[True, True, True],\n",
       "        [True, True, True],\n",
       "        [True, True, True]])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 9
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-23T06:59:53.811025Z",
     "start_time": "2025-06-23T06:59:53.795089Z"
    }
   },
   "cell_type": "code",
   "source": [
    "X = torch.arange(24).reshape(2, 3, 4)\n",
    "X"
   ],
   "id": "ea4b655970d273aa",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[[ 0,  1,  2,  3],\n",
       "         [ 4,  5,  6,  7],\n",
       "         [ 8,  9, 10, 11]],\n",
       "\n",
       "        [[12, 13, 14, 15],\n",
       "         [16, 17, 18, 19],\n",
       "         [20, 21, 22, 23]]])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 10
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-23T06:59:53.903344Z",
     "start_time": "2025-06-23T06:59:53.888093Z"
    }
   },
   "cell_type": "code",
   "source": [
    "A = torch.arange(20, dtype=torch.float32).reshape(5, 4)\n",
    "B = A.clone()\n",
    "A, A + B"
   ],
   "id": "ae7c37c0d0cf4021",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(tensor([[ 0.,  1.,  2.,  3.],\n",
       "         [ 4.,  5.,  6.,  7.],\n",
       "         [ 8.,  9., 10., 11.],\n",
       "         [12., 13., 14., 15.],\n",
       "         [16., 17., 18., 19.]]),\n",
       " tensor([[ 0.,  2.,  4.,  6.],\n",
       "         [ 8., 10., 12., 14.],\n",
       "         [16., 18., 20., 22.],\n",
       "         [24., 26., 28., 30.],\n",
       "         [32., 34., 36., 38.]]))"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 11
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-23T06:59:53.950047Z",
     "start_time": "2025-06-23T06:59:53.935671Z"
    }
   },
   "cell_type": "code",
   "source": "A * B",
   "id": "e14bbfdb458387d4",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[  0.,   1.,   4.,   9.],\n",
       "        [ 16.,  25.,  36.,  49.],\n",
       "        [ 64.,  81., 100., 121.],\n",
       "        [144., 169., 196., 225.],\n",
       "        [256., 289., 324., 361.]])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 12
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-23T06:59:54.027928Z",
     "start_time": "2025-06-23T06:59:54.013052Z"
    }
   },
   "cell_type": "code",
   "source": [
    "a = 2\n",
    "X = torch.arange(24).reshape(2, 3, 4)\n",
    "print(a + X)\n",
    "print(a * X)\n",
    "(a * X).shape"
   ],
   "id": "b9207c7a7a30247c",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[[ 2,  3,  4,  5],\n",
      "         [ 6,  7,  8,  9],\n",
      "         [10, 11, 12, 13]],\n",
      "\n",
      "        [[14, 15, 16, 17],\n",
      "         [18, 19, 20, 21],\n",
      "         [22, 23, 24, 25]]])\n",
      "tensor([[[ 0,  2,  4,  6],\n",
      "         [ 8, 10, 12, 14],\n",
      "         [16, 18, 20, 22]],\n",
      "\n",
      "        [[24, 26, 28, 30],\n",
      "         [32, 34, 36, 38],\n",
      "         [40, 42, 44, 46]]])\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "torch.Size([2, 3, 4])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 13
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-23T06:59:54.103928Z",
     "start_time": "2025-06-23T06:59:54.089993Z"
    }
   },
   "cell_type": "code",
   "source": [
    "x = torch.arange(4, dtype=torch.float32)\n",
    "x, x.sum()"
   ],
   "id": "468b24dd737e11fe",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(tensor([0., 1., 2., 3.]), tensor(6.))"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 14
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-23T06:59:54.149895Z",
     "start_time": "2025-06-23T06:59:54.135170Z"
    }
   },
   "cell_type": "code",
   "source": [
    "print(A)\n",
    "A.shape, A.sum(), A.shape[0], A.shape[1]"
   ],
   "id": "78dc4a70753e71ff",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[ 0.,  1.,  2.,  3.],\n",
      "        [ 4.,  5.,  6.,  7.],\n",
      "        [ 8.,  9., 10., 11.],\n",
      "        [12., 13., 14., 15.],\n",
      "        [16., 17., 18., 19.]])\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "(torch.Size([5, 4]), tensor(190.), 5, 4)"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 15
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-23T06:59:54.179834Z",
     "start_time": "2025-06-23T06:59:54.166033Z"
    }
   },
   "cell_type": "code",
   "source": [
    "A_sum_axis0 = A.sum(axis=0)\n",
    "A_sum_axis0, A_sum_axis0.shape"
   ],
   "id": "9b84e7c1cdf600c1",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(tensor([40., 45., 50., 55.]), torch.Size([4]))"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 16
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-23T06:59:54.209934Z",
     "start_time": "2025-06-23T06:59:54.195850Z"
    }
   },
   "cell_type": "code",
   "source": [
    "A_sum_axis1 = A.sum(axis=1)\n",
    "A_sum_axis1, A_sum_axis1.shape"
   ],
   "id": "890f7886fe9da28e",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(tensor([ 6., 22., 38., 54., 70.]), torch.Size([5]))"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 17
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-23T07:08:52.280236Z",
     "start_time": "2025-06-23T07:08:52.263372Z"
    }
   },
   "cell_type": "code",
   "source": [
    "B = torch.arange(24, dtype=torch.float32).reshape((2,3,4))\n",
    "B"
   ],
   "id": "86beaf4d9e6f56f6",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[[ 0.,  1.,  2.,  3.],\n",
       "         [ 4.,  5.,  6.,  7.],\n",
       "         [ 8.,  9., 10., 11.]],\n",
       "\n",
       "        [[12., 13., 14., 15.],\n",
       "         [16., 17., 18., 19.],\n",
       "         [20., 21., 22., 23.]]])"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 36
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-23T07:16:37.435491Z",
     "start_time": "2025-06-23T07:16:37.427869Z"
    }
   },
   "cell_type": "code",
   "source": "B.sum(axis=0),B.sum(axis=1),B.sum(axis=1,keepdim=True),B/B.sum(axis=1,keepdim=True)",
   "id": "1e78ac8247f57211",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(tensor([[12., 14., 16., 18.],\n",
       "         [20., 22., 24., 26.],\n",
       "         [28., 30., 32., 34.]]),\n",
       " tensor([[12., 15., 18., 21.],\n",
       "         [48., 51., 54., 57.]]),\n",
       " tensor([[[12., 15., 18., 21.]],\n",
       " \n",
       "         [[48., 51., 54., 57.]]]),\n",
       " tensor([[[0.0000, 0.0667, 0.1111, 0.1429],\n",
       "          [0.3333, 0.3333, 0.3333, 0.3333],\n",
       "          [0.6667, 0.6000, 0.5556, 0.5238]],\n",
       " \n",
       "         [[0.2500, 0.2549, 0.2593, 0.2632],\n",
       "          [0.3333, 0.3333, 0.3333, 0.3333],\n",
       "          [0.4167, 0.4118, 0.4074, 0.4035]]]))"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 42
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-23T06:59:54.256232Z",
     "start_time": "2025-06-23T06:59:54.242357Z"
    }
   },
   "cell_type": "code",
   "source": "A.sum(axis=[0]), A.sum(axis=[1]), A.sum(axis=[0, 1])  # 结果和A.sum()相同",
   "id": "7ebd4902181ef2b7",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(tensor([40., 45., 50., 55.]), tensor([ 6., 22., 38., 54., 70.]), tensor(190.))"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 18
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-23T06:59:54.287499Z",
     "start_time": "2025-06-23T06:59:54.273248Z"
    }
   },
   "cell_type": "code",
   "source": "A.mean(), A.sum() / A.numel()",
   "id": "6474657ee1de26d3",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(tensor(9.5000), tensor(9.5000))"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 19
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-23T06:59:54.318444Z",
     "start_time": "2025-06-23T06:59:54.304095Z"
    }
   },
   "cell_type": "code",
   "source": [
    "print(A)\n",
    "A.mean(axis=0), A.sum(axis=0), A.shape[0], A.sum(axis=0) / A.shape[0]"
   ],
   "id": "fd95245f635c4abc",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[ 0.,  1.,  2.,  3.],\n",
      "        [ 4.,  5.,  6.,  7.],\n",
      "        [ 8.,  9., 10., 11.],\n",
      "        [12., 13., 14., 15.],\n",
      "        [16., 17., 18., 19.]])\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "(tensor([ 8.,  9., 10., 11.]),\n",
       " tensor([40., 45., 50., 55.]),\n",
       " 5,\n",
       " tensor([ 8.,  9., 10., 11.]))"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 20
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-23T06:59:54.349788Z",
     "start_time": "2025-06-23T06:59:54.335680Z"
    }
   },
   "cell_type": "code",
   "source": [
    "sum_A = A.sum(axis=1, keepdims=True)\n",
    "A, sum_A, A.sum(axis=1)"
   ],
   "id": "11fe3f6a64bcfddf",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(tensor([[ 0.,  1.,  2.,  3.],\n",
       "         [ 4.,  5.,  6.,  7.],\n",
       "         [ 8.,  9., 10., 11.],\n",
       "         [12., 13., 14., 15.],\n",
       "         [16., 17., 18., 19.]]),\n",
       " tensor([[ 6.],\n",
       "         [22.],\n",
       "         [38.],\n",
       "         [54.],\n",
       "         [70.]]),\n",
       " tensor([ 6., 22., 38., 54., 70.]))"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 21
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-23T06:59:54.380704Z",
     "start_time": "2025-06-23T06:59:54.367075Z"
    }
   },
   "cell_type": "code",
   "source": "A / sum_A",
   "id": "d5af3657795d6765",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[0.0000, 0.1667, 0.3333, 0.5000],\n",
       "        [0.1818, 0.2273, 0.2727, 0.3182],\n",
       "        [0.2105, 0.2368, 0.2632, 0.2895],\n",
       "        [0.2222, 0.2407, 0.2593, 0.2778],\n",
       "        [0.2286, 0.2429, 0.2571, 0.2714]])"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 22
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-23T06:59:54.457800Z",
     "start_time": "2025-06-23T06:59:54.443183Z"
    }
   },
   "cell_type": "code",
   "source": "A, A.cumsum(axis=0), A.cumsum(axis=1)",
   "id": "2c3dd039bfa0f34e",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(tensor([[ 0.,  1.,  2.,  3.],\n",
       "         [ 4.,  5.,  6.,  7.],\n",
       "         [ 8.,  9., 10., 11.],\n",
       "         [12., 13., 14., 15.],\n",
       "         [16., 17., 18., 19.]]),\n",
       " tensor([[ 0.,  1.,  2.,  3.],\n",
       "         [ 4.,  6.,  8., 10.],\n",
       "         [12., 15., 18., 21.],\n",
       "         [24., 28., 32., 36.],\n",
       "         [40., 45., 50., 55.]]),\n",
       " tensor([[ 0.,  1.,  3.,  6.],\n",
       "         [ 4.,  9., 15., 22.],\n",
       "         [ 8., 17., 27., 38.],\n",
       "         [12., 25., 39., 54.],\n",
       "         [16., 33., 51., 70.]]))"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 23
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-23T06:59:54.534177Z",
     "start_time": "2025-06-23T06:59:54.519988Z"
    }
   },
   "cell_type": "code",
   "source": [
    "y = torch.ones(4, dtype=torch.float32)\n",
    "x, y, torch.dot(x, y)"
   ],
   "id": "8349c11f2bb24727",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(tensor([0., 1., 2., 3.]), tensor([1., 1., 1., 1.]), tensor(6.))"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 24
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-23T06:59:54.581455Z",
     "start_time": "2025-06-23T06:59:54.566845Z"
    }
   },
   "cell_type": "code",
   "source": "torch.sum(x * y)",
   "id": "cc9c8f67f849daa",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor(6.)"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 25
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-23T06:59:54.629161Z",
     "start_time": "2025-06-23T06:59:54.614889Z"
    }
   },
   "cell_type": "code",
   "source": "A, x, A.shape, x.shape, torch.mv(A, x)",
   "id": "920658f84c631cc3",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(tensor([[ 0.,  1.,  2.,  3.],\n",
       "         [ 4.,  5.,  6.,  7.],\n",
       "         [ 8.,  9., 10., 11.],\n",
       "         [12., 13., 14., 15.],\n",
       "         [16., 17., 18., 19.]]),\n",
       " tensor([0., 1., 2., 3.]),\n",
       " torch.Size([5, 4]),\n",
       " torch.Size([4]),\n",
       " tensor([ 14.,  38.,  62.,  86., 110.]))"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 26
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-23T06:59:54.675928Z",
     "start_time": "2025-06-23T06:59:54.660895Z"
    }
   },
   "cell_type": "code",
   "source": [
    "B = torch.ones(4, 3)\n",
    "A.shape, B, torch.mm(A, B), torch.mm(A, B).shape"
   ],
   "id": "1ee35ac3bacf20ef",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(torch.Size([5, 4]),\n",
       " tensor([[1., 1., 1.],\n",
       "         [1., 1., 1.],\n",
       "         [1., 1., 1.],\n",
       "         [1., 1., 1.]]),\n",
       " tensor([[ 6.,  6.,  6.],\n",
       "         [22., 22., 22.],\n",
       "         [38., 38., 38.],\n",
       "         [54., 54., 54.],\n",
       "         [70., 70., 70.]]),\n",
       " torch.Size([5, 3]))"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 27
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-23T06:59:54.706227Z",
     "start_time": "2025-06-23T06:59:54.691994Z"
    }
   },
   "cell_type": "code",
   "source": [
    "u = torch.tensor([3.0, -4.0])\n",
    "torch.norm(u) #L2范数"
   ],
   "id": "bf7584f873a013cf",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor(5.)"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 28
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-23T06:59:54.737630Z",
     "start_time": "2025-06-23T06:59:54.722485Z"
    }
   },
   "cell_type": "code",
   "source": "torch.abs(u).sum()  #L1范数",
   "id": "1d6c9a2baf903697",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor(7.)"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 29
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-23T06:59:54.785331Z",
     "start_time": "2025-06-23T06:59:54.770815Z"
    }
   },
   "cell_type": "code",
   "source": [
    "D = torch.ones((4, 9))\n",
    "D,torch.norm(D),torch.sum(D)"
   ],
   "id": "5bfc4fc34c69b48",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(tensor([[1., 1., 1., 1., 1., 1., 1., 1., 1.],\n",
       "         [1., 1., 1., 1., 1., 1., 1., 1., 1.],\n",
       "         [1., 1., 1., 1., 1., 1., 1., 1., 1.],\n",
       "         [1., 1., 1., 1., 1., 1., 1., 1., 1.]]),\n",
       " tensor(6.),\n",
       " tensor(36.))"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 30
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
